import jieba
import pymongo

client = pymongo.MongoClient(host='127.0.0.1', port=27017)
# db 表示数据库名称，username 用户名 password 密码
#client.db.authenticate(useranme, password)
# 建立数据库连接
db = client.CNNVD_vul # or db = client['test']
collection = db.vuldb # or collection = db['students']

# 查询所有漏洞名称
cursor = collection.find({}, {"vulName": 1})

print(cursor[0])
# 分词并写入corpus.txt文件
with open("corpus.txt", "w", encoding="utf-8") as file:
    for document in cursor:
        vul_name = document["vulName"]
        seg_list = jieba.cut(vul_name,cut_all=False)
        for word in seg_list:
            print(word)
            file.write(word + "\n")